import time
import requests
import unittest

text = "贺凯廷使用，当前时间为：" + time.strftime("%Y%m%D%H%M%S", time.localtime())

class MeetHereAPITest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.base_url = "http://118.121.200.162:8085/"
        cls.s=cls.login(cls)
        cls.glo = globals()

    def login(self):
       session = requests.Session()
       url = self.base_url + "loginCheck.do"
       userinfo ={
           "userID": "hekaiting",
           "password": "ting021011."
       }
       res = session.post(url, data=userinfo)
       assert 200 == res.status_code
       return session

    def test_MessageList(self):
        url = self.base_url + "message/getMessageList"
        res = requests.get(url)
        assert 200 == res.status_code

    def test_1sendMessage(self):
        url = self.base_url +"sendMessage"
        # text = "贺凯廷使用，当前时间为：" + time.strftime("%Y%m%D%H%M%S", time.localtime())
        self.glo["content"] = text
        message = {
            "userID":"hekaiting",
            "content":text
        }
        res = self.s.post(url, data=message)
        self.assertEqual(200,res.status_code)

    def test_2findUserList(self):
        url = self.base_url + "message/findUserList"
        data = {
            "page":1
        }
        res = self.s.get(url,params=data)
        self.assertEqual(200,res.status_code)
        self.assertIn(self.glo["content"],res.text)
        # global messageID
        messageID = res.json()[0]["messageID"]
        self.glo["msid"] = messageID

    def test_3delMessage(self):
        url = self.base_url + "delMessage.do"
        msid = {
            "messageID":self.glo["msid"]
        }
        res = self.s.post(url,data=msid)
        self.assertEqual(200,res.status_code)

if __name__ == '__main__':
    unittest.main()